package com.pengweiwei.androidzygote.main.ui;

import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.fragment.LoadableFragment;
import com.putong35.common.utils.ui.GlideImageLoader;
import com.putong35.common.utils.ui.HeaderContentLayoutWraper;
import com.pengweiwei.androidzygote.databinding.FragmentHomeBinding;
import com.pengweiwei.androidzygote.databinding.HeaderHomeBinding;
import com.pengweiwei.androidzygote.goods.HotGoodsFragment;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/12.
 */

public class HomeFragment extends LoadableFragment{

    FragmentHomeBinding fragmentHomeBinding;
    HotGoodsFragment hotGoodsFragment;
    HeaderContentLayoutWraper headerContentSI;
    HeaderHomeBinding headerHomeBinding;

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        fragmentHomeBinding = FragmentHomeBinding.inflate(inflater);
        headerHomeBinding = HeaderHomeBinding.inflate(inflater);
        hotGoodsFragment = new HotGoodsFragment();

        headerContentSI = new HeaderContentLayoutWraper(getContext(), fragmentHomeBinding.lytContent1);
        headerContentSI.getRefreshLayout().setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                reload();
            }
        });
        headerContentSI.addHeaderView(headerHomeBinding.getRoot());
        headerContentSI.addContentView(getChildFragmentManager(), hotGoodsFragment);

        fragmentHomeBinding.toolbar.setTitle("首页");
        return fragmentHomeBinding.getRoot();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        startLoading();
        return view;
    }

    @Override
    public void onStartLoading() {
        List<String> images = new ArrayList<>();
        images.add("http://or84r4lrb.bkt.clouddn.com/c3c4dfc8-6ae5-403b-abd9-a10ac8d9efff.jpg");
        images.add("http://or84r4lrb.bkt.clouddn.com/9101c1ee-8c92-49b1-83e8-1c3ea0600de1.jpg");
        images.add("http://or84r4lrb.bkt.clouddn.com/c3ec1dcc-6319-4077-9625-842160f95df0.jpg");
        images.add("http://or84r4lrb.bkt.clouddn.com/fe3b0121-60d7-4ed7-8254-0c417385422d.jpg");

        Banner banner = headerHomeBinding.banner;
        banner.setOnBannerListener(i -> {

        });
        banner.setImages(images).setImageLoader(new GlideImageLoader()).start();
        setState(CompState.DATA);

        if (headerContentSI.getRefreshLayout().isRefreshing()) {
            headerContentSI.getRefreshLayout().setRefreshing(false);
        }
    }

    public void reload() {
        hotGoodsFragment.reload();
        startLoading();
    }


}
